package tania.coca.ejercicios.equipos;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;

import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JTable;
import java.awt.Panel;
import java.awt.Button;
import java.awt.Rectangle;
import java.awt.Label;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JTextField;



public class Equipos extends JPanel {

	private DefaultTableModel model = new DefaultTableModel();
	private static final long serialVersionUID = 1L;
	private Panel panel = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable1 = null;
	private JButton jButton = null;
	private JTextField jTextField = null;
	private Label label = null;
	private Label label1 = null;
	private Label label2 = null;
	private JTextField jTextField1 = null;
	private JTextField jTextField2 = null;
	private JOptionPane jOptionPane = null;
	/**
	 * This is the default constructor
	 */
	public Equipos() {
		super();
		initialize();
		addColumns();
		jTable1.setModel(model);
	}

	private void addColumns(){
		
		model.addColumn("Nombre Equipo");
		model.addColumn("Ganados");
		model.addColumn("Perdidos");
		model.addColumn("AVG");
		model.addColumn("Diferencia");
		
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		label2 = new Label();
		label2.setBounds(new Rectangle(85, 308, 104, 23));
		label2.setText("Juegos Ganados:");
		label1 = new Label();
		label1.setBounds(new Rectangle(86, 269, 96, 23));
		label1.setText("Total de Juegos:");
		label = new Label();
		label.setBounds(new Rectangle(85, 231, 98, 23));
		label.setText("Nombre Equipo:");
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.fill = GridBagConstraints.BOTH;
		gridBagConstraints1.gridy = 0;
		gridBagConstraints1.weightx = 1.0;
		gridBagConstraints1.gridx = 0;
		this.setSize(585, 382);
		this.setLayout(null);
		this.add(getPanel(), null);
		this.add(getJScrollPane(), null);
		this.add(getJButton(), null);
		this.add(getJTextField(), null);
		this.add(label, null);
		this.add(label1, null);
		this.add(label2, null);
		this.add(getJTextField1(), null);
		this.add(getJTextField2(), null);
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	/**
	 * This method initializes panel	
	 * 	
	 * @return java.awt.Panel	
	 */
	private Panel getPanel() {
		if (panel == null) {
			panel = new Panel();
			panel.setLayout(new GridBagLayout());
			panel.setBounds(new Rectangle(0, 0, 0, 0));
		}
		return panel;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(32, 68, 392, 117));
			jScrollPane.setViewportView(getJTable1());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable1	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable1() {
		if (jTable1 == null) {
			jTable1 = new JTable();
		}
		return jTable1;
	}

	/**
	 * This method initializes jButton	
	 * @return 
	 * 	
	 * @return javax.swing.JButton	
	 */
	
	private int Divition(int a, int b){
		
		int div = (a*1000)/b;
		
		return div;
		
	}
	
	private int Diferencia(int a, int b){
		
		int dif = a - b;
		int result = Math.abs(dif);
		
		return dif;
	}
	
	private int Perdidos(int a, int b){
		
		int perd = b - a;
		
		return perd;
		
	}
	
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton("Buscar");
			jButton.setBounds(new Rectangle(146, 347, 84, 23));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				
					String equipo = jTextField.getText();
										
					if(equipo.equals("") || jTextField1.getText().equals("")|| jTextField2.getText().equals("")){
						getJOptionPane().showMessageDialog(null, "Llene los campos vacios","Campos Vacios",JOptionPane.ERROR_MESSAGE);
						
					
					}else{	
					
						int total = Integer.parseInt(jTextField1.getText());
						int ganados = Integer.parseInt(jTextField2.getText());						
						
						if(total<ganados){
							
							getJOptionPane().showMessageDialog(null, "El total de Juegos debe ser mayor o igual que los ganados", "Error Total Juegos ",JOptionPane.ERROR_MESSAGE);
						
						}
						else{
							
							Object[] objects = {equipo,Integer.parseInt(jTextField2.getText()),Perdidos(ganados, total), Divition(ganados, total), Diferencia(30, ganados)};
							model.addRow(objects);
							
							Organizar o = new Organizar(model);
							
							model = o.organizarArrayList(objects);
							
							jTable1.setModel(model);
							
						}
					}
				}
			}
		);
	}
	return jButton;
}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(204, 233, 128, 22));
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(206, 271, 68, 20));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setBounds(new Rectangle(207, 307, 65, 20));
		}
		return jTextField2;
	}
	
	private JOptionPane getJOptionPane() {
		if (jOptionPane == null) {
			jOptionPane = new JOptionPane();
			
		}
		return jOptionPane;
	}

}  //  @jve:decl-index=0:visual-constraint="9,30"
